﻿using System.Collections.Generic;
using System.ComponentModel;

namespace Utils.Wpf
{
	public abstract class ViewModelValidatedBase : ViewModelBase, IDataErrorInfo
	{
		#region Properties

		protected Dictionary<string, bool> ValidatedErrors { get; private set; }

		public bool HasValidationErrors
		{
			get
			{
				return this.ValidatedErrors.ContainsValue(true);
			}
		}

		#endregion

		#region Public Methods

		protected ViewModelValidatedBase()
		{
			this.ValidatedErrors = new Dictionary<string, bool>();
		}

		public string Error
		{
			get { throw new System.NotImplementedException(); }
		}

		public string this[string columnName]
		{
			get
			{
				if (!this.ValidatedErrors.ContainsKey(columnName))
				{
					this.ValidatedErrors.Add(columnName, false);
				}
				else
				{
					this.ValidatedErrors[columnName] = false;
				}

				return this.ValidateField(columnName);
			}
		}

		// The inheritor must implement the ValidateField method updating the ValidatedErrors property as needed
		protected abstract string ValidateField(string fieldName);

		#endregion
	}
}
